/**
 * 拼接请求后台地址
 * @param url
 * @returns {string}
 */
function appendHost(url) {
    let host = "http://localhost:80/merchant_api/";
    return host + url;
}

/**
 * 拼接请求前端页面地址
 * @param url
 * @returns {string}
 */
function appendBeforeHost(url) {
    let host = "http://localhost:80/views/merchant_view/";
    return host + url;
}


/**
 * 对返回结果进行统一处理
 * BaseRespVO
 */
function handlerResp(json, callback) {
    // console.log("handlerResp");
    // console.log(json);
    //返回json结果装换成对象
    // let resp = JSON.parse(json);
    let resp=json;
    //判断code状态
    if (resp.code == 100) {
        //成功，调用回调方法
        callback(resp.data);
    } else if (resp.code == 102) {
        //用户未登录
        location.href = "login.html";
    } else {
        //失败，统一提示错误信息
        alert(resp.msg);
    }
}

/**
 * 对返回结果进行统一处理
 * BaseRespVO
 */
function handlerResp1(json, callback) {
    // console.log("handlerResp");
    // console.log(json);
    //返回json结果装换成对象
    let resp = JSON.parse(json);
    // let resp=json;
    //判断code状态
    if (resp.code == 100) {
        //成功，调用回调方法
        callback(resp.data);
    } else if (resp.code == 102) {
        //用户未登录
        location.href = "login.html";
    } else {
        //失败，统一提示错误信息
        alert(resp.msg);
    }
}

//get 无参
function get(url, callback) {
    url = appendHost(url);
    $.ajax({
        type: "GET",
        url: url,
        async: false,
        dataType: 'json',
        contentType: "application/json;charset=UTF-8",
        success: function (resp) {
            handlerResp(resp, callback);
        }
    });
}

//get 有参
function getWithParams(url, params, callback) {
    url = appendHost(url);
    $.ajax({
        type: "get",
        url: url,
        dataType: 'json',
        contentType: "application/json;charset=UTF-8",
        data: params,
        success: function (resp) {
            handlerResp(resp, callback);
        }
    });
}

//post 有参(表单)
function postWithParams(url, params, callback) {
    url = appendHost(url);
    $.ajax({
        type: "post",
        url: url,
        data: params,
        success: function (resp) {
            handlerResp1(resp, callback);
        }
    });
}

//post 有参(JSON)
function postWithJson(url, params, callback) {
    url = appendHost(url);
    let paramsJson = JSON.stringify(params);
    $.ajax({
        type: "post",
        url: url,
        dataType: 'json',
        contentType: "application/json;charset=UTF-8",
        data: paramsJson,
        success: function (resp) {
            handlerResp(resp, callback);
        }
    });
}

/**
 * 根据参数名称，获取URL中的参数值
 * @param paramName 参数名称
 * @returns {*}
 */
function getUrlParam(paramName) {
    var query = decodeURI(window.location.search.substring(1));
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == paramName) {
            return pair[1];
        }
    }
    return null;
}

/**
 * cookie的存取删除操作
 */
//设置cookie(时间为分钟)
function SetCookie(name, value, time)//两个参数，一个是cookie的名子，一个是值
{
    var exp = new Date();    //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + time * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

//得到cookie
function getCookie(name)//取cookies函数
{
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null) return unescape(arr[2]);
    return null;
}

//删除cookie
function delCookie(name)//删除cookie
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}

/**
 * 校验用户是否已登录，没有登录跳转到登录页面
 * @returns {*} 用户信息对象
 */
function checkUserLogin() {
    let userMsg = getCookie("userMassage");
    if (userMsg == null) {
        console.log("未登录！");
        location.href = appendBeforeHost("/login.html"); //跳转登录页
        return null;
    } else {
        return JSON.parse(userMsg);
    }
}